package de.windeln.marketing.validation;


import org.joda.time.DateTime;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import de.windeln.marketing.campaign.model.RequestClientObject;

@Component
public class CampaignStartAfterEnd implements Validator{

	@Override
	public boolean supports(Class<?> clazz) {
		return clazz == RequestClientObject.class;
	}

	@Override
	public void validate(Object target, Errors errors) {
		RequestClientObject rco = (RequestClientObject) target;
		if(new DateTime(rco.getCampaignStart()).isAfter(new DateTime(rco.getCampaignEnd()))){
			errors.rejectValue("campaignStart", "Wrong Dates", "Campaign Start is not allowed to be after Campaign End!");
			errors.rejectValue("campaignEnd", "Wrong Dates", "Campaign End is not allowed to be before Campaign Start!");
		}
	}



}
